<template>
  <!-- 已关注的时候显示 -->
  <van-button
    v-if="isFollow"
    round
    size="small"
    :loading="btnloading"
    @click="changeFollow"
  >已关注</van-button>
  <!-- 未关注的时候显示 -->
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="btnloading"
    @click="changeFollow"
  >关注</van-button>
</template>

<script>
// 获取关注和取消关注接口
import { deleteFollow, addFollow } from '@/api/user'
export default {
  data () {
    return {
      btnloading: false // 控制关注按钮加载状态显示
    }
  },
  model: {
    prop: 'isFollow',
    event: 'updateFollow'
  },
  //  model: {
  //    prop: 'checked',
  //    event: 'change'
  // }
  props: {
    isFollow: {
      type: Boolean,
      required: true
    },
    userID: {
      type: [String, Number, Object],
      required: true
    }
  },
  methods: {
    // 点击切换关注状态
    async changeFollow () {
      this.btnloading = true
      try {
        if (this.isFollow) {
          await deleteFollow(this.userID)
        } else {
          await addFollow(this.userID)
        }
        // 获取到数据后让按钮按照实际显示
        this.btnloading = false
        this.$emit('updateFollow', !this.isFollow)
      } catch (err) {
        // 用户自己不能关注自己
        let msg = '操作失败,请稍后再试'
        if (err.response && err.response.status === 400) {
          msg = '自己不能关注自己'
        }
        this.$toast(msg)
      }
    }
  }
}
</script>

<style>

</style>
